package cn.stylefeng.guns.base.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: football_manage
 * @description:
 * @author: xiesm
 * @create: 2023-09-15 10:00
 **/
public class JsscUtil {

    //    private static final String url = "https://api.api68.com/pks/queryToDayNumberLawOfStatistics.do?date=&lotCode=10037";
    private static final String url = "https://www.168357.com/html/xyft/controller/xyftinfoList.php?lotCode=426";

    public static void main(String[] args) {
//        List<JsscModel> codes = parse();
////        System.out.println(codes);
////        for (int i = 1; i < 2; i++) {
////            String url = "https://api.apiose122.com/klsf/getHistoryLotteryInfo.do?date={}&lotCode=10055";
////            String date = DateUtil.format(new Date(), "yyyy-MM-dd");
////            String host = StrUtil.format(url, date, i);
////            String body = HttpUtil.createGet(host).execute().body();
////            System.out.println(host);
////            System.out.println(body);
////        }
//        String time = getTimeFromIssue(31092082, 31092200, "2023-11-07");
//        System.out.println(time);
//        List<JsscModel> parse = parse();
//        System.out.println(parse);
        String str = FileUtil.readString("D:\\home\\11.txt", Charset.defaultCharset());
        JSONObject jsonObject = JSONObject.parseObject(str);
        JSONArray datas = jsonObject.getJSONObject("result").getJSONArray("data");
        for (int i = datas.size() - 1; i >= 0; i--) {
            JSONObject data = datas.getJSONObject(i);
            String code = data.getString("preDrawCode");
            String[] codes = code.split(",");
            StringBuffer sb = new StringBuffer();
            sb.append(data.getString("preDrawIssue")).append("\t");
            for (int j = 0; j < codes.length; j++) {
                sb.append(codes[j]).append("\t");
            }
            System.out.println(sb.toString());
        }
    }

    public static JSONObject get(int offset) {
        DateTime dateTime = DateUtil.offsetDay(new Date(), offset);
        String date = DateUtil.format(dateTime, "yyyy-MM-dd");
        String host = StrUtil.format(url, date);
        System.out.println(host);
        String body = HttpUtil.createGet(host)
                .timeout(5000)
                .execute().body();
        return JSONObject.parseObject(body);
    }

    public static JSONObject get() {
        return get(0);
    }

    public static List<JsscModel> parse() {
        return parse(0);
    }

    public static List<JsscModel> parse(int offset) {
        JSONObject data = get(offset);
        JSONArray array = data.getJSONObject("result").getJSONArray("data");
//        Collections.reverse(array);

        List<JsscModel> codes = new ArrayList<>();

//        codes.add(new JsscModel("31187103", new String[]{"04", "14",  "13", "04", "05", "06", "08", "07"}));
//        codes.add(new JsscModel("31187102", new String[]{"11", "06",  "04", "15", "06", "17", "08", "09"}));
//        codes.add(new JsscModel("31187101", new String[]{"03", "04",  "05", "06", "17", "08", "19", "10"}));
//        codes.add(new JsscModel("31177003", new String[]{"13", "05",  "06", "07", "08", "09", "10", "11"}));
//        codes.add(new JsscModel("31177002", new String[]{"05", "06",  "07", "08", "09", "10", "11", "12"}));
//
//        codes.add(new JsscModel("31177001", new String[]{"06", "07",  "08", "04", "05", "11", "12", "13"}));
//        codes.add(new JsscModel("31077000", new String[]{"08", "2",  "1", "1", "1", "1", "1", "1"}));
//        codes.add(new JsscModel("31076999", new String[]{"16", "5",  "2", "2", "2", "2", "2", "2"}));
//        codes.add(new JsscModel("31076998", new String[]{"18", "9",  "5", "5", "5", "5", "5", "5"}));
//        codes.add(new JsscModel("31076997", new String[]{"04", "7",  "1", "1", "1", "1", "1", "1"}));
//
//        codes.add(new JsscModel("31076996", new String[]{"19", "13", "3", "3", "3", "3", "3", "3"}));
//        codes.add(new JsscModel("31076995", new String[]{"20", "15", "4", "4", "4", "4", "4", "4"}));
//        codes.add(new JsscModel("31076994", new String[]{"21", "20", "7", "7", "7", "7", "7", "7"}));
//        codes.add(new JsscModel("31076993", new String[]{"12", "19", "9", "9", "9", "9", "9", "9"}));
//        codes.add(new JsscModel("31076992", new String[]{"23", "11", "8", "8", "8", "8", "8", "8"}));

        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            String preDrawCode = jsonObject.getString("preDrawCode");
            String preDrawIssue = jsonObject.getString("preDrawIssue");
            String[] preDrawCodes = preDrawCode.split(",");
            JsscModel JsscModel = new JsscModel(preDrawIssue, preDrawCodes);
            codes.add(JsscModel);
        }
        return codes;
    }

    //计算时间
    public static String getTimeFromIssue(Integer beginIssue, Integer endIssue, String date) {
        date += " 00:04:00";
        DateTime dateTime1 = DateUtil.parse(date);
        int num = endIssue - beginIssue;
        DateTime dateTime = DateUtil.offsetMinute(dateTime1, num * 5);
        String str = DateUtil.format(dateTime, "HH:mm");
        return str;
    }

}
